//
//  HChartView.swift
//  Drop
//
//  Created by hushaohua on 2018/4/5.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class HLinearChartConfiguration : HChartViewConfiguration{
    var keyAxisPref:HChartAxisPreference?;
    var valueAxisPref:HChartAxisPreference?;
    var valuePref:HChartAxisPreference?;
    
    var xBaselineColor:UIColor = UIColor.lightGray;
    var yBaselineColor:UIColor = UIColor.lightGray;
    
    func xAxisEdge() -> CGFloat{
        return self.valueAxisHeight();
    }
    func yAxisEdge() -> CGFloat{
        return self.keyAxisHeight();
    }
    func keyAxisHeight() -> CGFloat{
        if (self.keyAxisPref != nil){
            return self.keyAxisPref!.textWidth;
        }
        return 0;
    }
    func valueAxisHeight() -> CGFloat{
        if (self.valueAxisPref != nil){
            return self.valueAxisPref!.textWidth;
        }
        return 0;
    }
    
    func valueHeight() -> CGFloat{
        if (self.valuePref != nil){
            return self.valuePref!.textWidth;
        }
        return 0;
    }
}

class HChartAxisPreference{
    var textWidth:CGFloat = 12;
    var textColor:UIColor = UIColor.black;
    var font:UIFont = UIFont.systemFont(ofSize: 10, weight: .light);
    var lineColor:UIColor = UIColor.gray;
    var lineWidth:CGFloat = 1;
    
    var textAttributes:[NSAttributedStringKey:Any]{
        let style:NSMutableParagraphStyle = NSMutableParagraphStyle();
        style.alignment = NSTextAlignment.center;
        let attrs:[NSAttributedStringKey:Any] = [
            .font: font,
            .foregroundColor: textColor,
            .paragraphStyle: style
        ];
        return attrs;
    }
}

class HLinearChartView: HChartView {
    
    override func chartConfiguration() -> HChartViewConfiguration {
        return self.linearConfiguration();
    }
    
    var maxValue:Int64 = 0;
    func getMaxValue() -> Void {
        self.maxValue = 0;
        for chart in self.chartItems! {
            if chart.value > self.maxValue{
                self.maxValue = chart.value;
            }
        }
        //TODO:
        if (self.maxValue % 3600 != 0){
            self.maxValue = (self.maxValue / 3600 + 1) * 3600;
        }
        if ((self.maxValue / 3600) % 2 != 0){
            self.maxValue = (self.maxValue / 3600 + 1) * 3600;
        }
        if ((self.maxValue / 3600) % 4 != 0){
            let value = CGFloat(self.maxValue / 3600) / 4.0;
            for index in 1...4 {
                let string:NSString = NSString(format: "%.1f", (value * CGFloat(index)));
                self.valueTexts.append(string);
            }
        }else{
            let value:Int = Int((self.maxValue / 3600) / 4);
            for index in 1...4 {
                let string:NSString = NSString(format: "%ld", (value * index));
                self.valueTexts.append(string);
            }
        }
    }
    
    var valueTexts:[NSString] = {
        var titles = [NSString]();
        return titles;
    }();
    
    func drawXBaselines(){
        let context:CGContext = UIGraphicsGetCurrentContext()!;
        context.setLineDash(phase: 0, lengths: [10, 10]);
        context.setLineWidth(1);
        let config = self.linearConfig();
        var offsetY:CGFloat = 0;
        for index in 0...3 {
            offsetY = CGFloat(index) * (self.bounds.size.height - config.yAxisEdge()) / 4;
            context.move(to: CGPoint(x:config.xAxisEdge(), y:offsetY));
            context.addLine(to: CGPoint(x:self.bounds.size.width,y:offsetY));
        }
        config.xBaselineColor.setStroke();
        context.drawPath(using: .stroke);
    }
    
    func drawValueTexts() {
        let config = self.linearConfig();
        var offsetY:CGFloat = 0;
        for index in 0...3 {
            offsetY = CGFloat(index) * (self.bounds.size.height - config.keyAxisHeight()) / 4;
            var rect:CGRect = .zero;
            rect.origin.x = 0;
            rect.origin.y = offsetY - 3;
            rect.size.width = config.valueAxisHeight();
            rect.size.height = config.valueAxisPref!.font.pointSize + 2;
            let string:NSString = self.valueTexts[3 - index];
            string.draw(in: rect, withAttributes: config.valueAxisPref!.textAttributes);
        }
    }
    
    func drawYBaselines(){
        
    }
    
    var chartItems:[HChartItem]?{
        didSet{
            self.prepareChartItemsData();
            self.setNeedsDisplay();
        }
    }
    
    func prepareChartItemsData() {
        self.getMaxValue();
    }
    
    func linearConfiguration() -> HLinearChartConfiguration{
        let config = HLinearChartConfiguration();
        config.keyAxisPref = HChartAxisPreference();
        config.valueAxisPref = HChartAxisPreference();
        return config;
    }
    
    func linearConfig() -> HLinearChartConfiguration{
        return self.configuration as! HLinearChartConfiguration;
    }
    
    func drawKeyAxis(){
        let config = self.linearConfig();
        if (config.keyAxisPref != nil){
            let context:CGContext = UIGraphicsGetCurrentContext()!;
            context.beginPath();
            context.move(to: CGPoint(x:config.valueAxisHeight(),y:self.bounds.size.height - config.keyAxisHeight()));
            context.addLine(to: CGPoint(x:self.bounds.size.width,y:self.bounds.size.height - config.keyAxisHeight()));
            context.closePath();
            context.setLineWidth(config.keyAxisPref!.lineWidth);
            config.keyAxisPref!.lineColor.setStroke();
            context.drawPath(using: CGPathDrawingMode.stroke);
        }
    }
    
    func drawValues(){
        if (self.linearConfig().valuePref != nil){
            for chart in self.chartItems! {
                let string:NSString = NSString(string: chart.valueString);
                string.draw(in: chart.valueRect, withAttributes: self.linearConfig().valuePref!.textAttributes);
            }
        }
    }
    
    func drawKeyTexts() {
        for chart in self.chartItems! {
            let string:NSString = NSString(string: chart.key);
            string.draw(in: chart.keyRect, withAttributes: self.linearConfig().keyAxisPref!.textAttributes);
        }
    }
    
    func drawValueAxis(){
        let config = self.linearConfig();
        if (config.valueAxisPref != nil){
            let context:CGContext = UIGraphicsGetCurrentContext()!;
            context.beginPath();
            context.move(to: CGPoint(x:config.valueAxisHeight(),y:0));
            context.addLine(to: CGPoint(x:config.valueAxisHeight(),y:self.bounds.size.height - config.keyAxisHeight()));
            context.closePath();
            context.setLineWidth(config.valueAxisPref!.lineWidth);
            self.linearConfig().valueAxisPref!.lineColor.setStroke();
            context.drawPath(using: CGPathDrawingMode.stroke);
        }
    }
}
